/*
 * Copyright 2024 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.gitee.joyz.api.result;

import io.gitee.joyz.api.result.i18n.ResourceBundleLocaleMessage;

import java.util.Locale;
import java.util.function.Supplier;

/**
 * @author JoyZ
 */
public class ResultContextHolder extends ResourceBundleLocaleMessage {

    /**
     * 默认枚举
     */
    private ResultantStatus defaultStatus;

    /**
     * 语言环境.
     * 当每次解析消息时, 系统都会动态的获取该语言环境实例.
     * 可以用于适配多语言应
     */
    private Supplier<Locale> localeSupplier;


    /**
     * 获取设置的语言环境供应商实例
     *
     * @return 返回设置的语言环境供应商实例
     */
    public Locale getLocale() {
        if (localeSupplier != null) {
            return localeSupplier.get();
        }
        return null;
    }

    public ResultantStatus getDefaultStatus() {
        return defaultStatus;
    }

    public void setDefaultStatus(ResultantStatus defaultStatus) {
        this.defaultStatus = defaultStatus;
    }

    public Supplier<Locale> getLocaleSupplier() {
        return localeSupplier;
    }

    public void setLocaleSupplier(Supplier<Locale> localeSupplier) {
        this.localeSupplier = localeSupplier;
    }

    /**
     * 禁用构造器
     */
    private ResultContextHolder() {
    }

    /**
     * 获取 ResultContext 实例
     *
     * @return 返回 ResultContext 实例
     */
    public static ResultContextHolder instance() {
        return SingletonBuilder.INSTANCE.getInstance();
    }

    /**
     * 单例构建器
     */
    private enum SingletonBuilder {
        /**
         * 枚举项
         */
        INSTANCE;

        /**
         * {@link ResultContextHolder} 实例
         */
        private final ResultContextHolder context;

        SingletonBuilder() {
            context = new ResultContextHolder();
        }

        public ResultContextHolder getInstance() {
            return context;
        }
    }


}
